package com.warski.wonderfires.view;

import android.content.Context;
import android.content.res.TypedArray;
import android.graphics.*;
import android.util.AttributeSet;
import androidx.annotation.Nullable;
import androidx.appcompat.widget.AppCompatImageView;
import com.warski.wonderfires.R;

/**
 * Created by xuhaiping on 2020/8/29.
 * 一个圆角图片控件
 */
public class RoundImageView extends AppCompatImageView {
    private int cornerSize;
    private Paint paint;
    private int bgcolor;

    public RoundImageView(Context context) {
        this(context, null);
    }

    public RoundImageView(Context context, @Nullable AttributeSet attrs) {
        super(context, attrs);

        TypedArray ta = context.obtainStyledAttributes(attrs, R.styleable.RoundImageView);
        cornerSize = ta.getInt(R.styleable.RoundImageView_radius, 12);
        bgcolor = ta.getColor(R.styleable.RoundImageView_bgcolor, Color.WHITE);

        paint = new Paint();
        paint.setColor(bgcolor);
        paint.setAntiAlias(true);
    }

    @Override
    public void draw(Canvas canvas) {
        super.draw(canvas);
        drawLeftTop(canvas);
        drawRightTop(canvas);
        drawLeftBottom(canvas);
        drawRightBottom(canvas);
    }

    private void drawLeftTop(Canvas canvas) {
        Path path = new Path();
        path.moveTo(0, cornerSize);
        path.lineTo(0, 0);
        path.lineTo(cornerSize, 0);
        path.arcTo(new RectF(0, 0, cornerSize * 2, cornerSize * 2), -90, -90);
        path.close();
        canvas.drawPath(path, paint);
    }

    private void drawLeftBottom(Canvas canvas) {
        Path path = new Path();
        path.moveTo(0, getHeight() - cornerSize);
        path.lineTo(0, getHeight());
        path.lineTo(cornerSize, getHeight());
        RectF oval = new RectF(0, getHeight() - cornerSize * 2, cornerSize * 2, getHeight());
        path.arcTo(oval, 90, 90);
        path.close();
        canvas.drawPath(path, paint);
    }

    private void drawRightBottom(Canvas canvas) {
        Path path = new Path();
        path.moveTo(getWidth() - cornerSize, getHeight());
        path.lineTo(getWidth(), getHeight());
        path.lineTo(getWidth(), getHeight() - cornerSize);
        RectF oval = new RectF(getWidth() - cornerSize * 2, getHeight() - cornerSize * 2, getWidth(), getHeight());
        path.arcTo(oval, 0, 90);
        path.close();
        canvas.drawPath(path, paint);
    }

    private void drawRightTop(Canvas canvas) {
        Path path = new Path();
        path.moveTo(getWidth(), cornerSize);
        path.lineTo(getWidth(), 0);
        path.lineTo(getWidth() - cornerSize, 0);
        RectF oval = new RectF(getWidth() - cornerSize * 2, 0, getWidth(), 0 + cornerSize * 2);
        path.arcTo(oval, -90, 90);
        path.close();
        canvas.drawPath(path, paint);
    }

    /**
     * 获取圆角
     * @return
     */
    public int getCornerSize() {
        return cornerSize;
    }

    /**
     * 设置圆角
     * @param cornerSize
     */
    public void setCornerSize(int cornerSize) {
        this.cornerSize = cornerSize;
    }
}
